package com.lzlclass.conf;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.lzlclass.dao.ConditionalClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/*
*
* proxyBeanMethods = false  配置文件中的bean组件，使用时才创建对象【lite】 ****
*proxyBeanMethods = true  配置文件中的bean组件，使用时才创建对象【full】
*
* */

@Configuration(proxyBeanMethods = false)
public class DuridConfig {

//  注入自定义数据源操作

    @Bean
//    @Conditional 筛选组件,条件判断
    @ConditionalOnMissingBean
    public ConditionalClass getConditionalClass(){
        return new ConditionalClass();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

//后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//        配置初始化参数
        Map<String,String> map = new HashMap<>();
//        配置登陆的用户密码
        map.put("loginUsername","admin");
        map.put("loginPassword","123456");
//          配置水可以访问
        map.put("allow","");
        bean.setInitParameters(map);
        return bean;
    }
}
